package com.ytjj.qmyx.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.gexin.fastjson.JSON;
import com.huifu.adapay.Adapay;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.huifu.adapay.model.AdapayCommon;
import com.huifu.adapay.model.MerConfig;
import com.huifu.adapay.model.Payment;
import com.ytjj.qmyx.admin.service.adapay.AdaPayService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * 
 * @describe
 */
@RestController
@RequestMapping("/testAdapayController")
public class TestAdapayController {
    @Resource
    private AdaPayService adaPayService;

    @GetMapping("/testAdapay")
    public void testAdapay(String pamentId){
//        adaPayService.executeRefund("1","0.01","1");
        try {
            Map<String, Object> stringObjectMap = adaPayService.queryRefund(pamentId, null, null);
            List<Map> refunds = com.alibaba.fastjson.JSON.parseArray(com.alibaba.fastjson.JSON.toJSONString(stringObjectMap.get("refunds")), Map.class);
            if(!CollectionUtils.isEmpty(refunds)){
                for (Map refund : refunds) {
                    String transStatus =(String) refund.get("trans_status");
                    if("S".equals(transStatus)){
                        System.out.println("已退款！！！！");
                    }
                }
            }
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/alipayTest")
    public void alipayTest(){
        try {
            /**
             * debug 模式，开启后有详细的日志
             */
//            Adapay.debug = true;

            /**
             * prodMode 模式，默认为生产模式，false可以使用mock模式
             */
//            Adapay.prodMode = true;

            /**
             * 初始化商户配置，服务器启动前，必须通过该方式初始化商户配置完成
             * apiKey为prod模式的API KEY
             * mockApiKey为mock模式的API KEY
             * rsaPrivateKey为商户发起请求时，用于请求参数加签所需要的RSA私钥
             */
//            String apiKey = "api_live_d9cfbe4f-aea3-4f68-b30c-9ffa179f6cd7";
//            String mockApiKey = "api_test_3ffd245d-38da-419f-aec4-ff11dc29a2df";
//            String rsaPrivateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALwpa7MoBv9yY4iV5A6XLpAdTA9SDQq/OfE6xPjjH97qs6ejihzCCzGdC0nIIxHpm8D6dYnPN6ZVOnjKBJ0t1ZKNfbUDuGC6+7/1UiRg/UwX+qA/8DcoQ2HwZvXkJYWXTXum2FEJ9UfMUbWh6sfhCNJplgXYFy7TWnzw4QyiqxgzAgMBAAECgYBEQFIMnkxLhFaOSLB8imIdbEMWm5CXgV2nHsYr8VayKTgZarzCSbB9wpVBohL2gGI60Z6o6j0pXGjlP4IE8YcyDUd2vMCQvI8tS0JH5lZ3leAMjGWWMaKKiQKkwAobPHl3Cvd5fx5W2gH4JUFEPcvjI2ZLoUTinkIxT7UVSbP1oQJBAPbIHwBLGuDL39fzCUyd03HPjqkQpV5EsVokHlZxi0lzQ+PD9urJQW7uy4sM92Y5LmwHi78AyYdkr8wxwX4SXBcCQQDDML1h5lfYjRJbqfAKRIc0frIr5JTbFo1QCOLPfgLvQA3uQevc1j1TbVDKZDz0dL2KyXVSPl8PV/nV6o4jl6pFAkAShNX+uaQctm/nzq8l9roFUmQMP9mJB3KfpxI3vnGLY4D2uJ/w4oVsg7L2jlSKcUQQi0hrXu3AoUen9RvmEA6lAkBTQMm2JRpY6irnX/9bUcldcaCrKg73t5HEjlW3bKbxxx9p/UIN6m8z009/U+vP1K83sB/TWXkQQVT1kdrYgYsFAkB8yMhQOJwPZ4L1HjrtbifyKPFQqRgOPN4JjNXm2dY5Dt/rPbGTSJnYOimFVGZPFMOpP0ZHKyVqHa1yGXJXV5Gq";
//            MerConfig merConfig = new MerConfig();
//            merConfig.setApiKey(apiKey);
//            merConfig.setApiMockKey(mockApiKey);
//            merConfig.setRSAPrivateKey(rsaPrivateKey);
//            Adapay.initWithMerConfig(merConfig);

//            String merchantKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8KWuzKAb/cmOIleQOly6QHUwPUg0KvznxOsT44x/e6rOno4ocwgsxnQtJyCMR6ZvA+nWJzzemVTp4ygSdLdWSjX21A7hguvu/9VIkYP1MF/qgP/A3KENh8Gb15CWFl017pthRCfVHzFG1oerH4QjSaZYF2Bcu01p88OEMoqsYMwIDAQAB";
//            Map<String, Object> paymentParams = new HashMap<String, Object>(10);
//            paymentParams.put("app_id", "app_c76bf9ba-d40d-4e04-bad4-eae9a5e72472");
//            paymentParams.put("order_no", "jsdk_payment"+System.currentTimeMillis());
//            paymentParams.put("pay_channel", "alipay");
//            paymentParams.put("pay_amt", "0.01");
//            paymentParams.put("goods_title", "test");
//            paymentParams.put("goods_desc", "test");
//            paymentParams.put("div_members", "");
//
//            Map<String, Object> device_info = new HashMap<String, Object>();
//            device_info.put("device_ip","127.0.0.1");
//            paymentParams.put("device_info",device_info);
//
//            Map<String, Object> payment = Payment.create(paymentParams);
//            System.out.println("payment result="+JSON.toJSONString(payment));

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("order_no",  "adapay_04");
            params.put("adapay_func_code", "wxpay.createOrder");
            params.put("pay_amt", "0.01");
            params.put("app_id", "app_c76bf9ba-d40d-4e04-bad4-eae9a5e72472");
            params.put("currency",  "cny");
            params.put("goods_title",  "测试");
            params.put("goods_desc",  "测试");
            params.put("description",  "测试");
            params.put("callback_url",  "http://localhost:8080/pages/order/orderDetails.html?orderId=ORDER_ID&payOrderId=PAY_ID");
//            params.put("pay_mode","delay");
            List<Map<String,Object>> list = new ArrayList<>();
            Map<String,Object> divMembers = new HashMap<>();
            divMembers.put("amount","0.01");
            divMembers.put("fee_flag","Y");
            divMembers.put("member_id","0");
            list.add(divMembers);
            params.put("div_members", Arrays.asList(divMembers));
            Map<String, Object> response = AdapayCommon.requestAdapayUits(params);
            System.out.println(JSON.toJSONString(response));
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        try {
            /**
             * debug 模式，开启后有详细的日志
             */
            Adapay.debug = true;

            /**
             * prodMode 模式，默认为生产模式，false可以使用mock模式
             */
            Adapay.prodMode = true;

            /**
             * 初始化商户配置，服务器启动前，必须通过该方式初始化商户配置完成
             * apiKey为prod模式的API KEY
             * mockApiKey为mock模式的API KEY
             * rsaPrivateKey为商户发起请求时，用于请求参数加签所需要的RSA私钥
             */
            String apiKey = "api_live_d9cfbe4f-aea3-4f68-b30c-9ffa179f6cd7";
            String mockApiKey = "api_test_3ffd245d-38da-419f-aec4-ff11dc29a2df";
            String rsaPrivateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALwpa7MoBv9yY4iV5A6XLpAdTA9SDQq/OfE6xPjjH97qs6ejihzCCzGdC0nIIxHpm8D6dYnPN6ZVOnjKBJ0t1ZKNfbUDuGC6+7/1UiRg/UwX+qA/8DcoQ2HwZvXkJYWXTXum2FEJ9UfMUbWh6sfhCNJplgXYFy7TWnzw4QyiqxgzAgMBAAECgYBEQFIMnkxLhFaOSLB8imIdbEMWm5CXgV2nHsYr8VayKTgZarzCSbB9wpVBohL2gGI60Z6o6j0pXGjlP4IE8YcyDUd2vMCQvI8tS0JH5lZ3leAMjGWWMaKKiQKkwAobPHl3Cvd5fx5W2gH4JUFEPcvjI2ZLoUTinkIxT7UVSbP1oQJBAPbIHwBLGuDL39fzCUyd03HPjqkQpV5EsVokHlZxi0lzQ+PD9urJQW7uy4sM92Y5LmwHi78AyYdkr8wxwX4SXBcCQQDDML1h5lfYjRJbqfAKRIc0frIr5JTbFo1QCOLPfgLvQA3uQevc1j1TbVDKZDz0dL2KyXVSPl8PV/nV6o4jl6pFAkAShNX+uaQctm/nzq8l9roFUmQMP9mJB3KfpxI3vnGLY4D2uJ/w4oVsg7L2jlSKcUQQi0hrXu3AoUen9RvmEA6lAkBTQMm2JRpY6irnX/9bUcldcaCrKg73t5HEjlW3bKbxxx9p/UIN6m8z009/U+vP1K83sB/TWXkQQVT1kdrYgYsFAkB8yMhQOJwPZ4L1HjrtbifyKPFQqRgOPN4JjNXm2dY5Dt/rPbGTSJnYOimFVGZPFMOpP0ZHKyVqHa1yGXJXV5Gq";
            MerConfig merConfig = new MerConfig();
            merConfig.setApiKey(apiKey);
            merConfig.setApiMockKey(mockApiKey);
            merConfig.setRSAPrivateKey(rsaPrivateKey);
            Adapay.initWithMerConfig(merConfig);

//            String merchantKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8KWuzKAb/cmOIleQOly6QHUwPUg0KvznxOsT44x/e6rOno4ocwgsxnQtJyCMR6ZvA+nWJzzemVTp4ygSdLdWSjX21A7hguvu/9VIkYP1MF/qgP/A3KENh8Gb15CWFl017pthRCfVHzFG1oerH4QjSaZYF2Bcu01p88OEMoqsYMwIDAQAB";
            Map<String, Object> paymentParams = new HashMap<String, Object>(10);
            paymentParams.put("app_id", "app_c76bf9ba-d40d-4e04-bad4-eae9a5e72472");
            paymentParams.put("order_no", "jsdk_payment"+System.currentTimeMillis());
            paymentParams.put("pay_channel", "alipay");
            paymentParams.put("pay_amt", "0.01");
            paymentParams.put("goods_title", "test");
            paymentParams.put("goods_desc", "test");
            paymentParams.put("div_members", "");

            Map<String, Object> device_info = new HashMap<String, Object>();
            device_info.put("device_ip","127.0.0.1");
            paymentParams.put("device_info",device_info);

            Map<String, Object> payment = Payment.create(paymentParams);
            System.out.println("payment result="+JSON.toJSONString(payment));
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
